﻿using PLCSharp.CoreModel.Prism;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;


namespace PLCSharp.Views.Connects
{
    public class NewConnectViewModel : DialogAwareBase
    {

        private string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                SetProperty(ref _Name, value);
            }
        }
        private ProtocolType _Type;
        public ProtocolType Type
        {
            get { return _Type; }
            set
            {
                SetProperty(ref _Type, value);

                switch (value)
                {
                    case ProtocolType.SocketClient:
                        ConnectStr = "远端IP：";
                        break;
                    case ProtocolType.SocketSever:
                        ConnectStr = "本端IP：";
                        break;
                    case ProtocolType.ModbusTcpClient:
                        ConnectStr = "远端IP：";
                        break;
                    case ProtocolType.ModbusTcpServer:
                        ConnectStr = "本端IP：";
                        break;
                    case ProtocolType.SerialPort:
                        ConnectStr = "串口：";
                        break;
                    default:
                        break;
                }
            }
        }

        private string _ConnectStr;
        /// <summary>
        /// Comment
        /// </summary>
        public string ConnectStr
        {
            get { return _ConnectStr; }
            set { SetProperty(ref _ConnectStr, value); }
        }
        private string _Ip;
        public string IP
        {
            get { return _Ip; }
            set
            {
                SetProperty(ref _Ip, value);
            }
        }
        private int _Port;
        public int Port
        {
            get { return _Port; }
            set
            {
                SetProperty(ref _Port, value);
            }
        }

        private string _Comment;

        public NewConnectViewModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
        {
        }

        /// <summary>
        /// Comment
        /// </summary>
        public string Comment
        {
            get { return _Comment; }
            set { SetProperty(ref _Comment, value); }
        }
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            Name = Guid.NewGuid().ToString().Substring(1, 5);
            Type = ProtocolType.SocketClient;
            IP = "127.0.0.1";
            Comment = "新连接";
            Port = 7000;

            Title = "New Connect";
        }
        public override void RaiseRequestClose(IDialogResult dialogResult)
        {
            dialogResult.Parameters.Add("IP", IP);
            dialogResult.Parameters.Add("Port", Port);
            dialogResult.Parameters.Add("Name", Name);
            dialogResult.Parameters.Add("Type", Type);
            dialogResult.Parameters.Add("Comment", Comment);

            base.RaiseRequestClose(dialogResult);
        }
    }
}
